import tracker from '../utils/tracker.js';
import getLastEvent from '../utils/getLastEvent.js';
import getSelector from '../utils/getSelector.js';
export default function longTask() {
  new PerformanceObserver((entryList, observer) => {
    entryList.getEntries().forEach(entry => {
      if (entry.duration > 150) {
        let lastEvent = getLastEvent();
        var path = lastEvent.path || (function (evt) {
          var path = [];
          var el = evt.target;
          while (el) {
            path.push(el);
            el = el.parentElement;
          }
          return path;
        })(lastEvent);
        console.log("longTask:", getSelector(path))
        tracker.send({
          kind: 'experience',
          type: 'longTask',
          eventType: lastEvent.type,
          startTime: entry.startTime, // 开始时间
          duration: entry.duration,  // 持续时间
          selector: lastEvent ? getSelector(path) : "",
        })
      }
    })
  }).observe({ entryTypes: ['longtask'] })
}